<template>
  <div class="title">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/index/goods' }"
        >商品管理页面</el-breadcrumb-item
      >
      <el-breadcrumb-item :to="{ path: '/index/goodsadd'}">新品上线</el-breadcrumb-item>
    </el-breadcrumb>
  </div>
  <el-card class="good-container">
    <template #header>
      <div class="header">
        <el-button type="primary" @click="$router.push( '/index/goodsadd')"
          >新增商品</el-button
        >
      </div>
    </template>
    <el-table
      :data="state.tableData"
      tooltip-effect="dark"
      style="width: 100%"
    >
      <el-table-column prop="goodsid" label="商品编号"> </el-table-column>
      <el-table-column prop="goodsname" label="商品名"> </el-table-column>
      <el-table-column prop="goodsdetails" label="商品简介"> </el-table-column>
      <el-table-column label="商品图片" width="150px">
        <template #default="scope">
          <img
            style="width: 100px; height: 100px"
            :key="scope.row.goodsId"
            src="https://cdn.cnbj0.fds.api.mi-img.com/b2c-shopapi-pms/pms_1660187331.91357446.jpg"
            alt="商品主图"
          />
        </template>
      </el-table-column>
      <el-table-column prop="goodsnum" label="商品库存"> </el-table-column>
      <el-table-column prop="goodsprice" label="商品售价"> </el-table-column>
      <el-table-column label="上架状态">
        <template #default="scope">
          <span style="color: green" v-if="scope.row.goodsstate == 0"
            >销售中</span
          >
          <span style="color: red" v-else>已下架</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" width="100">
        <template #default="scope">
          <el-button :append-to-body="true" style="cursor: pointer text;" 
          @click="goodsEdit(scope.row.goodsid)" >修改</el-button>
          <el-button  style="cursor: pointer text" 
          @click="ShopDel(scope.row)" >删除</el-button>
          <el-button class="xiabtn" style="cursor: pointer text" 
          v-if="scope.row.goodsstate == 0"
          @click="ChangeStatus(scope.row)" >下架</el-button>
          <el-button  style="cursor: pointer text" 
          v-else
          @click="ChangeStatus(scope.row)" >上架</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="state.total"
      :page-size="state.pageSize"
      :current-page="state.currentPage"
      :page-sizes="state.pageSizes"
      @size-change="sizeChange"
      @current-change="changePage"
    />
  </el-card>
</template>

<script setup>
import { onMounted, ref} from "vue";
// import { ElMessage, ElMessageBox} from 'element-plus'
import { useRouter, useRoute } from 'vue-router'
import {goods_shoplist,goods_status,goods_del} from '../../utils/api'
import axios from "axios";
const router = useRouter()

//创建响应式数据
const state = ref({
  tableData: [],
  total: 0,
  pageSize: 10,
  currentPage: 1,
  pageSizes: [3, 5, 10, 20, 50, 100, 200, 300, 400, 500, 1000], //可选择的一页多少条
});
var ShoplistData = ref({ list: [] });
// 发请求接受数据
onMounted(() => {
  goodslist();
});
const goodslist = () => {
  goods_shoplist().then((res) => {
    ShoplistData.value.list = res.data.shoplist;
    console.log(ShoplistData.value.list);
    goodslistpage();
  });
};
const goodslistpage = () =>{
  state.value.tableData = ShoplistData.value.list.slice(
      (state.value.currentPage - 1) * state.value.pageSize,
      state.value.currentPage * state.value.pageSize,
    );
    state.value.total = ShoplistData.value.list.length;
}
const changePage = (val) => {
  console.log("翻页，当前为第几页", val);
  state.value.currentPage = val;
  goodslist();
};
const sizeChange = (val) => {
  console.log("改变每页多少条，当前一页多少条数据", val);
  state.value.pageSize = val;
  state.value.currentPage = 1;
  goodslist();
};

// 商品删除
const ShopDel = (row) => {
  goods_del({id:row.goodsid}).then((res) =>{
  // 提示框
  ElMessageBox.confirm(
  "你确定要删除这一个商品嘛?",
  "删除",
  {
    confirmButtonText: "好的",
    cancelButtonText: "返回",
    type: "提示"
  }
)
  .then(() => {
    // 调用函数改变列表
    goodslist();
    ElMessage({
      type: "success",
      message: "你成功删除了一个商品！"
    })
  })
  .catch(() => {
    ElMessage({
      type: "info",
      message: "你没有删除任何商品！"
    })
  })
  })
}



// 上架，下架，改变
const ChangeStatus = (row) => {
  goods_status({id:row.goodsid }).then((res) =>{
    goodslist();
}
)}
// 修改数据
const goodsEdit = (id) => {
  router.push({ path: '/index/goodsedit', query: {id}  })
}

</script>

<style lang="scss" scoped>
.title {
  height: 20px;
  /* font-weight: bold; */
  line-height: 20px;
  text-align: center;
  margin: 10px;
}
.good-container {
  min-height: 100%;
}
.el-card.is-always-shadow {
  min-height: 100% !important;
}

.el-table  .el-button{
  width: 70px;
  height: 30px;
  margin: 5px 0 ;
  margin-right: 20px !important;
  color: black; 
  border: 1px solid #4CAF50;
  text-align: center;
  line-height: 30px;
  &:nth-child(2){
    border: 1px solid lightcoral;
  }
}
.el-table  .el-button:hover {
  background-color: #4CAF50;
  color: white;
  &:nth-child(2){
    background-color: lightcoral;
  }
}
.el-message-box__status {
  position: absolute !important;
}
.xiabtn{
  border: 1px solid lightcoral !important;
}
.xiabtn:hover {
  background-color: lightcoral !important;
}
</style>